<?php

namespace app\index\model;

use think\facade\Db;
use think\Model;

class Category extends Model
{
    public function article()
    {
        return $this->hasMany(Article::class);
    }
    public function list()
    {
        // 需求 : 得到 一级分类 及 二级分类 和 该 三级分类下的所有文章数量和今日发布数量
        $list = $this->listTree();
        foreach ($list as $k => $v) {
            foreach ($v['child'] as $m => $n) {
                $cateId = [];
                // 二级分类 id $v['id']
                array_push($cateId, $n['id']);
                foreach ($n['child'] as $l => $s) {
                    // 三级分类 id $n['id']
                    array_push($cateId, $s['id']);
                }
                $list[$k]['child'][$m]['other'] =  $this->other($cateId);
            }
            // $list[$k]['other'] = $this->other($cateId);
        }
        return $list;
    }
    // 二级分类额外参数 -> 总发布 | 今日发布
    public function other($cateId)
    {
        $total = Article::whereIn('category_id', $cateId)->count();
        $todayNum = Article::whereDay('create_time')->whereIn('category_id', $cateId)->where('status', 1)->count();
        return [
            'total' => $total,
            'todayNum' => $todayNum
        ];
    }
 
    public function listTree($pid = 0)
    {
        if ($pid == 0) {
            $cate = $this->where(['pid' => 0, 'is_show' => 1])->order('sort')->select();
        } else {
            $cate = $this->where(['pid' => $pid, 'is_show' => 1])->order('sort')->select();
        }
        foreach ($cate as $k => $v) {
            $cate[$k]['child'] = $this->listTree($v['id']);
        }
        return $cate;
    }
    // 分类详细页展示 -- 单独三级数组 
    public function threeChild($id) //PId
    {
        $list = [];
        $cate = $this->listTree();
        foreach ($cate as $k => $item) {
            foreach ($item['child'] as $s => $l) {
                foreach ($l['child'] as $i => $m) {
                    if ($m != null && $m != '' && $m['pid'] == $id) {
                        $list[] = $m;
                    }
                }
            }
        }
        return $list;
    }
    /** 判断是否为第三级分类
     * @param int id 分类ID
     */
    public function isThree($id)
    {
        $has = $this->findOrEmpty($id)->isEmpty();
        if ($has) return '不存在此分类';
        $data = $this->find($id);
        if ($data['pid'] != 0) {
            $two = $this->find($data['pid']);
            if (!empty($two)) {
                if ($two['pid'] != 0) {
                    return 1;
                } else {
                    return '该分类id不符合请求';
                }
            }
        } else {
            return '该分类id不符合请求';
        }
    }
    // 提交三级父类pid 得到一级数组父类名称
    public function getParentCate($pid)
    {
        $list = $this->listTree();
        $cateName = '';
        foreach ($list as $k => $v) {
            $cate = $v->category;
            foreach ($v->child as $m => $n) {
                foreach ($n->child as $j => $l) {
                    if ($l->id == $pid) {
                        $cateName = $cate;
                        break;
                    }
                }
            }
        }
        return $cateName;
    }
    // 分类关注
    public function follow($user_id)
    {
        $followCateId = Db::name('cateFollow')->where(['user_id' => $user_id])->select();
        $idArr = [];
        foreach ($followCateId as $k => $v) {
            if ($v['delete_time'] != 0) {
                $idArr[] = $v['cate_id'];
            }
        }
        $idArr = implode(',', $idArr);
        $list = self::whereIn('id', $idArr)->where('is_show', 1)->select();
        foreach ($list as $m => $n) {
            $cateId = [];
            // 二级分类 id $v['id']
            array_push($cateId, $n['id']);
            // 三级分类
            $threeChild = self::where(['pid' => $n['id']])->select();
            foreach ($threeChild as $l => $s) {
                // 三级分类 id $n['id']
                array_push($cateId, $s['id']);
            }
            $list[$m]['other'] =  $this->other($cateId);
        }
        return $list;
    }
    /*  public  function getParentCate($pid = 0)
    {
        $list = $this->list($pid);
        foreach ($list as $k => $v) {
            if ($v['child']) {
                if ($list[$k]['id'] == $pid) {
                    $cate =  $list[$k]['category'];
                    return $cate;
                }
                $cate = $this->getParentCate($v->child->pid);
            }
        }
        return $cate;
    } */
}
